this.name			= "PlanetFall_Taxi";
this.author			= "Thargoid, with orientation bits nicked from Eric Walch";
this.copyright		= "Creative Commons: attribution, non-commercial, sharealike.";
this.description		= "Simple taxi mission for PlanetFall";
this.version		= "1.0.1";

this.startUp = function()
	{ 
	log(this.name + " " + this.version + " loaded.");
	if (0 < oolite.compareVersion("1.72")) 
		{ 
		// 1.71.x or earlier 
		this.thePlayerShip = player; 
		} 
	else 
		{ 
		// 1.72 or later 
		this.thePlayerShip = player.ship; 
		}
	}

this.reset = function()
	{
	if(!missionVariables.planetFallTaxiOffer)	missionVariables.planetFallTaxiOffer = "For_Hire";
	if(!missionVariables.planetFallTaxiSystem) missionVariables.planetFallTaxiSystem = system.ID;
	if(!missionVariables.planetFallTaxiGalaxy) missionVariables.planetFallTaxiGalaxy = galaxyNumber;
	}

this.guiScreenChanged = function()
	{
	if(this.thePlayerShip.docked && guiScreen == "GUI_SCREEN_MANIFEST" && !system.isInterstellarSpace)
		{ 
		if(missionVariables.planetFallTaxiOffer == "For_Hire" && !this.thePlayerShip.dockedStation.hasRole("planetFall_noTrade") && Math.random() < 0.25 ) // 25% chance of a fare if you're in the right place and not already on one.
			{
			missionVariables.planetFallTaxiFee = 10 + (Math.ceil(Math.random()*40)); // fee of 11-50 credits
			this.pickupPoint = this.thePlayerShip.dockedStation
			this.whereTo = Math.random();
			missionVariables.planetFallTaxiOffer = "Under_Offer";

			if(this.whereTo < 0.25) // to the prime planet
				{
				if(worldScripts.PlanetFall.lastPlanetType == "Prime" && !this.pickupPoint.isMainStation) // if we're already on the Prime planet, go to the main station instead
					{
					this.destination = "Station";
					missionVariables.planetFallTaxiFee = (Math.ceil(missionVariables.planetFallTaxiFee / 2 )); // halve the fee, as it's a short hop
					}
				else 
					{
					this.destination = "Prime";
					}
				}

			if(this.whereTo >= 0.25 && this.whereTo < 0.5) // to the system station
				{
				if(!this.pickupPoint.isMainStation)
					{
					this.destination = "Station";
					}
				else // if we're already at the system main station, go to the Prime surface instead
					{
					missionVariables.planetFallTaxiFee = (Math.ceil(missionVariables.planetFallTaxiFee / 2 )); // halve the fee, as it's a short hop
					this.destination = "Prime";
					}
				}

			if(this.whereTo >= 0.5 && this.whereTo < 0.75) // to a sub-planet, if it exists
				{
				if(worldScripts.PlanetFall.planetCount == 1 || worldScripts.PlanetFall.lastPlanetType == "Sub")
				// if we're already on a sub planet, or there aren't any in the system
					{
					if(!this.pickupPoint.isMainStation)
						{
						this.destination = "Station";
						}
					else
						{
						this.destination = "Prime";
						}
					}
				else 
					{
					this.destination = "Sub";
					}
				}

			if(this.whereTo >= 0.75) // to a moon, if it exists
				{
				if(worldScripts.PlanetFall.moonCount == 0 || worldScripts.PlanetFall.lastPlanetType == "Moon")
				// if we're already on the moon, or there aren't any in the system
					{
					if(!this.pickupPoint.isMainStation)
						{
						this.destination = "Station";
						}
					else
						{
						this.destination = "Prime";
						}
					}
				else 
					{
					this.destination = "Moon";
					}					
				}

			switch(this.destination)
				{
				case "Prime":
					{
					mission.runMissionScreen("PlanetFall_Taxi_to_Prime", "PlanetFall_Taxi_Backdrop.png", "PlanetFall_Taxi_Prime_Choice");
					break;
					}
				case "Station":
					{
					mission.runMissionScreen("PlanetFall_Taxi_to_Station", "PlanetFall_Taxi_Backdrop.png", "PlanetFall_Taxi_Station_Choice");
					break;
					}
				case "Sub":
					{
					mission.runMissionScreen("PlanetFall_Taxi_to_Sub", "PlanetFall_Taxi_Backdrop.png", "PlanetFall_Taxi_Sub_Choice");
					break;
					}
				case "Moon":
					{
					mission.runMissionScreen("PlanetFall_Taxi_to_Moon", "PlanetFall_Taxi_Backdrop.png", "PlanetFall_Taxi_Moon_Choice");
					break;
					}
				}
			}

		if(missionVariables.planetFallTaxiOffer == "Hired")
			{
			this.systemCheck();
			if(this.sameSystem)
				{
				switch(this.destination)
					{
					case "Prime":					
						{
						if(worldScripts.PlanetFall.lastPlanetType == "Prime" && this.thePlayerShip.dockedStation.hasRole("planetFall_surface"))
							{
							player.credits += missionVariables.planetFallTaxiFee;
							missionVariables.planetFallTaxiOffer = "For_Hire";
							mission.setInstructionsKey(null)
							mission.runMissionScreen("PlanetFall_Arrived_Prime", "PlanetFall_Taxi_Backdrop.png", null);	
							}
						break;
						}
					case "Station":
						{
						if(this.thePlayerShip.dockedStation.isMainStation)
							{
							player.credits += missionVariables.planetFallTaxiFee;
							missionVariables.planetFallTaxiOffer = "For_Hire";
							mission.setInstructionsKey(null)
							mission.runMissionScreen("PlanetFall_Arrived_Station", "PlanetFall_Taxi_Backdrop.png", null);
							}
						break;
						}
					case "Sub":
						{
						if(worldScripts.PlanetFall.lastPlanetType == "Sub" && this.thePlayerShip.dockedStation.hasRole("planetFall_surface"))
							{
							player.credits += missionVariables.planetFallTaxiFee;
							missionVariables.planetFallTaxiOffer = "For_Hire";
							mission.setInstructionsKey(null)
							mission.runMissionScreen("PlanetFall_Arrived_Sub", "PlanetFall_Taxi_Backdrop.png", null);
							}
						break;
						}
					case "Moon":
						{
						if(worldScripts.PlanetFall.lastPlanetType == "Moon" && this.thePlayerShip.dockedStation.hasRole("planetFall_surface"))
							{
							player.credits += missionVariables.planetFallTaxiFee;
							missionVariables.planetFallTaxiOffer = "For_Hire";
							mission.setInstructionsKey(null)
							mission.runMissionScreen("PlanetFall_Arrived_Moon", "PlanetFall_Taxi_Backdrop.png", null);
							}
						break;
						}
					}
				}
			}
		}

	if(this.thePlayerShip.docked && guiScreen == "GUI_SCREEN_STATUS" && !system.isInterstellarSpace && missionVariables.planetFallTaxiOffer == "Worried")
		{
		missionVariables.planetFallTaxiOffer = "For_Hire";
		mission.setInstructionsKey(null)
		mission.runMissionScreen("PlanetFall_Wrong_System", "PlanetFall_Taxi_Backdrop.png", null);		
		}
	}

this.missionScreenEnded = function ()
	{
	if(missionVariables.planetFallTaxiOffer == "Under_Offer")
		{
		if(mission.choice == "PlanetFall_Taxi_Yes") // hire accepted
			{
			this.shortDesc = "PlanetFall_Taxi_Short_" + this.destination;
			missionVariables.planetFallTaxiOffer = "Hired";
			missionVariables.planetFallTaxiSystem = system.ID;
			missionVariables.planetFallTaxiGalaxy = galaxyNumber;
			mission.setInstructionsKey(this.shortDesc);
			}
		else if(mission.choice == "PlanetFall_Taxi_No") // hire rejected
			{
			missionVariables.planetFallTaxiOffer = "For_Hire";
			}
		mission.choice = null
		}
	}

this.playerStartedJumpCountDown = this.playerStartedJumpCountdown = function(type) // to get around trunk bug in 1.72.1
	{
	if(missionVariables.planetFallTaxiOffer == "Hired")
		{	
		missionVariables.planetFallTaxiOffer = "Worried";
		}
	}

this.playerCancelledJumpCountdown = function()
	{
	this.systemCheck();
	if(this.sameSystem && missionVariables.planetFallTaxiOffer == "Worried")
		{
		player.commsMessage("Thanks, I only wanted to go locally!", 6);
		missionVariables.planetFallTaxiOffer = "Hired";
		}
	}

this.shipExitedWitchspace = function()
	{
	if(missionVariables.planetFallTaxiOffer == "Worried")
		{
		this.systemCheck();
		if(!this.sameSystem)
			{
			player.consoleMessage("Hey, where are you taking me Commander " + player.name + "?", 6);
			}
		else
			{
			player.consoleMessage("Ah, we went via the scenic route!", 6);
			missionVariables.planetFallTaxiOffer = "Hired";
			}
		}
	}


this.systemCheck = function()
	{
	if(missionVariables.planetFallTaxiSystem == system.ID && missionVariables.planetFallTaxiGalaxy == galaxyNumber)
		{
		this.sameSystem = true;
		}
	else
		{
		this.sameSystem = false;
		}
	}

this.shipLaunchedFromStation = function(station)
	{
	if(missionVariables.planetFallTaxiOffer == "Under_Offer") // if something has auto-launched the player before a decision is made
		{
		missionVariables.planetFallTaxiOffer = "For_Hire";
		mission.setBackgroundImage(null);
		}
	}

